/*
 * CreateMeetingService.java
 *
 * Created on 02 March 2006, 15:44
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package web.services;

//import business.pojos.Meeting;
import java.util.ArrayList;
import java.util.Collection;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import se392.ateam2006.resourcemgmt.room.RoomEntity;
import web.util.BusinessFacadeDelegate;

/**
 *
 * @author Phil
 */
public class CreateMeetingService extends CreateService {   
    
    
    /** Creates a new instance of CreateMeetingService */
    public CreateMeetingService() {
    }
    
    public String executePreDispatch(HttpServletRequest request,
            ServletContext context)
            throws Exception {
                HttpSession session = request.getSession();
                BusinessFacadeDelegate delegate = this.getDelegate(request.getSession());
        
                if (null == session.getAttribute(Service.ATTRIBUTE_EXCEPTION)) {
                    String email = delegate.getLoggedInUser().getEmail();
                    String[] splitEmail = email.split("@");
                    String uid = splitEmail[0] + "_" + new Date().getTime();
                    session.setAttribute(MEETING_ID, uid);
                    session.setAttribute(MEETING_TITLE, "");
                    session.setAttribute(MEETING_DURATION_DAY, "0");
                    session.setAttribute(MEETING_DURATION_HOUR, "0");
                    session.setAttribute(MEETING_DURATION_MIN, "0");
                    session.setAttribute(MEETING_DESCRIPTION, "");
                    session.setAttribute(MEETING_START_DATE, "");
                    session.setAttribute(MEETING_END_DATE, "");                   
                    session.setAttribute(MEETING_ROOM_SIZE, "");
                    
                    Collection<RoomEntity> rooms = delegate.getAllRoomsAllowed(delegate.getLoggedInUser());
                    Collection<String> roomNames = new ArrayList<String>(); 
                    for(RoomEntity room: rooms) {
                        roomNames.add(room.getRoomID());
                    }
                    session.setAttribute(MEETING_VENUE, roomNames);
                } else {
                    session.setAttribute(MEETING_ID, request.getParameter(MEETING_ID));
                    session.setAttribute(MEETING_TITLE, request.getParameter(MEETING_TITLE));
                    session.setAttribute(MEETING_DURATION_DAY, request.getParameter(MEETING_DURATION_DAY));
                    session.setAttribute(MEETING_DURATION_HOUR, request.getParameter(MEETING_DURATION_HOUR));
                    session.setAttribute(MEETING_DURATION_MIN, request.getParameter(MEETING_DURATION_MIN));
                    session.setAttribute(MEETING_DESCRIPTION, request.getParameter(MEETING_DESCRIPTION));
                    session.setAttribute(MEETING_START_DATE, request.getParameter(MEETING_START_DATE));
                    session.setAttribute(MEETING_END_DATE, request.getParameter(MEETING_END_DATE));                   
                    session.setAttribute(MEETING_ROOM_SIZE, request.getParameter(MEETING_ROOM_SIZE));
                    
                    Collection<RoomEntity> rooms = delegate.getAllRoomsAllowed(delegate.getLoggedInUser());
                    Collection<String> roomNames = new ArrayList<String>(); 
                    for(RoomEntity room: rooms) {
                        roomNames.add(room.getRoomID());
                    }
                    session.setAttribute(MEETING_VENUE, roomNames);
                }

                return "update";
    }
    
    public void executePostDispatch(HttpServletRequest request,
            ServletContext context)
            throws Exception {
        
    }
}